package com.lunm.im.server;

import com.lunm.im.factory.HandlerFactory;
import com.lunm.im.massage.GroupCreateRequestMessage;
import com.lunm.im.protocol.IMLengthFieldBasedFrameDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;


/**
 * @program: mi-server
 * @author: Shawn
 * @create: 2024-12-28 17:26
 */
@Slf4j
public class Server {
    public static void main(String[] args) {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();

        try {
            Channel channel = new ServerBootstrap()
                    .channel(NioServerSocketChannel.class)
                    .group(boss, worker)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new IMLengthFieldBasedFrameDecoder());
                            ch.pipeline().addLast(HandlerFactory.getLoggingHandler());
                            ch.pipeline().addLast(HandlerFactory.getMessageCodec());
                            //用来判断是不是 读空闲时间过长，或 写空闲时间过长
                            //5s 内如果没有收到 channel 的数据，会触发一个 IdleState#READER_IDLE 事件
                            //参数 读空闲时间、写空闲时间、读写空闲时间
                            ch.pipeline().addLast(new IdleStateHandler(5, 0, 0));
                            // ChannelDuplexHandler 可以同时作为入站和出站处理器
                            ch.pipeline().addLast(new ChannelDuplexHandler() {
                                // 用来触发特殊事件
                                @Override
                                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                                    if (evt instanceof IdleStateEvent event) {
                                        // 触发了读空闲事件
                                        if (event.state() == IdleState.READER_IDLE) {
                                            log.debug("已经 5s 没有读到数据了");
                                            ctx.channel().close();
                                        }
                                    }

                                }
                            });
                            ch.pipeline().addLast(HandlerFactory.getLoginRequestHandler());
                            ch.pipeline().addLast(HandlerFactory.getChatRequestHandler());
                            ch.pipeline().addLast(HandlerFactory.getGroupCreateRequestHandler());
                            ch.pipeline().addLast(HandlerFactory.getGroupChatRequestHandler());
                            ch.pipeline().addLast(HandlerFactory.getGroupMemberRequestHandler());
                            ch.pipeline().addLast(HandlerFactory.getGroupJoinRequestHandler());
                            ch.pipeline().addLast(HandlerFactory.getGroupQuitRequestHandler());
                        }
                    }).bind(8080).sync().channel();
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            log.error("client error", e);
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}
